package com.algrithom.array;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.stream.Collectors;

/**
 * 189. 旋转数组
 * 
 * @author think
 */
public class Solution5 {
    
    public static void main(String[] args){
        int[] nums = new int[]{1,2,3,4,5,6,7};
        Solution5 solution5 = new Solution5();
        solution5.rotate(nums,3);
        System.out.println(Arrays.toString(nums));
    }
    
    public void rotate(int[] nums,int k){
        int len = nums.length;
        if (len == 1) {
            return;
        }
        LinkedList<Integer> list = Arrays.stream(nums).boxed().collect(Collectors.toCollection(LinkedList::new));
        while (k > 0) {
            list.offerFirst(list.pollLast());
            k--;
        }
        System.arraycopy(list.stream().mapToInt(Integer::intValue).toArray(),0,nums,0,len);
    }
    
    public void rotate2(int[] nums,int k){
        int n = nums.length;
        int[] newArr = new int[n];
        for (int i = 0; i < n; ++i) {
            newArr[(i + k) % n] = nums[i];
        }
        System.arraycopy(newArr,0,nums,0,n);
    }
}
